package com.xdf.whiteaccount.utils;

import com.xdf.whiteaccount.entity.DyeGreyDetail;
import com.xdf.whiteaccount.entity.Jrkbillsum;
import com.xdf.whiteaccount.entity.Plan;
import com.xdf.whiteaccount.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Optional;

@Component
public class SystemTypeConverter {
    @Autowired
    private PlanService planService;// 白坯计划
    /**
     * 将白坯打卷明细数据转换为白坯发货明细对象
     * @param jrkbillsum
     * @return
     */
    public DyeGreyDetail jrkbillsum2DyeGreyDetail(Jrkbillsum jrkbillsum) throws Exception {
        Integer planid = Optional.ofNullable(jrkbillsum.getPlanid()).orElse(0);// 计划单主键 = plan.id
        Plan plan = planService.selectByPrimaryKey(Long.valueOf(planid));
        if(plan == null) throw new BusinessException("没有找到主键" + planid + "的白坯计划！");

        DyeGreyDetail dyeGreyDetail = new DyeGreyDetail();
        dyeGreyDetail.setContract(plan.getContractCode());
        dyeGreyDetail.setOrderCode(plan.getOrderCode());
        dyeGreyDetail.setGoodsName(plan.getProductName());
        dyeGreyDetail.setMSpecification(plan.getMaterialSpecification());
        dyeGreyDetail.setMBatch(plan.getMaterialLotNo());
        dyeGreyDetail.setMHeader(plan.getGreyfabricSpecification());
        dyeGreyDetail.setMHeight(plan.getmHeight());
        dyeGreyDetail.setWhiteMf(plan.getGreyfabricWidth());
        dyeGreyDetail.setWhiteMc(plan.getMeterLength());
        dyeGreyDetail.setWhiteKz(plan.getGreyfabricWeight());
        dyeGreyDetail.setPiece(BigDecimal.valueOf(jrkbillsum.getPs()));
        dyeGreyDetail.setKilo(jrkbillsum.getQty());
        dyeGreyDetail.setState(1);
        dyeGreyDetail.setJrksumId(jrkbillsum.getId());
        dyeGreyDetail.setJrksumShelf(jrkbillsum.getBBarcode());
        return dyeGreyDetail;
    }
}
